# --------------------------------------------------------------------------------
#  NoiseTube Web application
#  
#  Copyright (C) 2008-2010 SONY Computer Science Laboratory Paris
#  Portions contributed by Vrije Universiteit Brussel (BrusSense team), 2008-2011
# --------------------------------------------------------------------------------
#  This library is free software; you can redistribute it and/or modify it under
#  the terms of the GNU Lesser General Public License, version 2.1, as published
#  by the Free Software Foundation.
#  
#  This library is distributed in the hope that it will be useful, but WITHOUT
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
#  details.
#  
#  You should have received a copy of the GNU Lesser General Public License along
#  with this library; if not, write to:
#    Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor,
#    Boston, MA  02110-1301, USA.
#  
#  Full GNU LGPL v2.1 text: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
#  NoiseTube project source code repository: http://code.google.com/p/noisetube
# --------------------------------------------------------------------------------
#  More information:
#   - NoiseTube project website: http://www.noisetube.net
#   - Sony Computer Science Laboratory Paris: http://csl.sony.fr
#   - VUB BrusSense team: http://www.brussense.be
# --------------------------------------------------------------------------------
 
class Cartasur::ObjectivesController < Cartasur::ApplicationController
  include Geokit::Geocoders
  before_filter :authenticate_user, :only => [:index, :show, :create, :edit, :update  ]
  def index

    @cusers = Cartasur::User.where("login like 'cartasur%'").order("users.id").paginate(:per_page => 10, :page => params[:page])

    #@cuser = Cartasur::User.find(params[:id])
    @cobjectives = Cartasur::Objective.order(:id).paginate(:per_page => 15, :order=>"id", :page => params[:page])

  end

  def new
    @objective = Cartasur::Objective.new
    @objective.protocols.build
    @locations = Cartasur::Location.all

  end


  def show
    #get the users objectives
    @user = current_user.login unless current_user.nil?
    @locations = Cartasur::Location.all
  end

  def create
    objectives_hash = params[:cartasur_objective]
    unless objectives_hash.nil?
        @locations = Cartasur::Location.all
         @objective = Cartasur::Objective.new(objectives_hash)
         if @objective.save
           flash[:notice] = "The objective was successfully saved! <a href='/cartasur/objectives/new'>Create another</a>".html_safe
           redirect_to @objective
         else
           flash[:alert] = "There was a problem when saving"
           render :action => 'new'
         end
    end

  end

  def edit
    @objective  = Cartasur::Objective.find(params[:id])
    @locations = Cartasur::Location.all
  end
  def update
    @objective = Cartasur::Objective.find(params[:id])

    if @objective.update_attributes(params[:cartasur_objective])
      flash[:notice] = "The objective was successfully updated!"
      redirect_to "/cartasur/objectives/"
    else
      flash[:alert] = "There was a problem when updating"
    end
  end

  def showusers
    #@user  = Cartasur::User.find(params[:id])
    #@cobjectives = @cuser.objectives
    @objective  = Cartasur::Objective.find(params[:id])
    @objectiveusers = @objective.users
  end

  def weeklyreport
     @weekno = 1
     @weekno = params[:wk].try(:to_i) unless params[:wk].nil?

    contributing_users = Cartasur::User.where(" last_activity_at is not null  ").order(" last_activity_at desc ")

    @info = contributing_users.map{|user|
      visiteds = Cartasur::Visited.where("user_id = #{user.id}").where(:created_at => @weekno.week.ago.beginning_of_week..@weekno.week.ago.end_of_week)

      measure_info = visiteds.map{|visited|
        track = Cartasur::Track.find_by_id(visited.track_id)
        if track.blank?
          msize = 0
        else
          msize = track.measures.try(:size)
        end
        loc = Cartasur::Location.find(visited.protocol.objective.location).try(:name)
        {measures: msize, location: loc, visitedid: visited.id}
      }

      {user: user.login, protocols: visiteds.try(:size), info: measure_info }
    }
    # sort them
    @info = @info.sort_by {|elt| elt[:user].split("asur").map(&:to_i).inject(&:+)}

  end
  ############# AUTHENTICATE USER #######################
  def authenticate_user
    unless logged_in?
      redirect_to '/cartasur/login' unless logged_in?
    else
      flash["notice"] = "You are not authorized to view the previous page"
      redirect_to cartasur_user_path(current_user) unless current_user.role == "admin"
    end
  end
end
